
import { spawnCreep } from "../../../role/spawn";
import { errLog } from "../../../util/log";
import { GroupTerminal } from "../terminal_group";

export class GroupExplorers extends GroupTerminal{
    /**
     * 
     * @param {Room} spawnRoom 
     * @param {RoomPosition | {pos:RoomPosition}} target
     * 
     */
    constructor(spawnRoom=undefined, target=undefined){
        super(spawnRoom);

        // this.reached=false;
        if(target!==undefined){
            if(target instanceof RoomPosition){
                this.targetPos=target;
            }else if(target.pos instanceof RoomPosition){
                this.targetPos=target.pos;
            }else{
                throw new Error('target type unknown');
            }
        }
    }
    serialize(){
        return {
            super:super.serialize(), 
            targetPos:this.targetPos,
        };
    }
    deserialize(obj){
        super.deserialize(obj.super);
        this.targetPos=obj.targetPos;
        return this;
    }
    get maxSize(){
        return 1;
    }
    get reached(){
        return Game.rooms[this.targetPos.roomName] !== undefined;
    }
    /**
     * 
     * @param {Flag|undefined} flag 
     * @returns {CreepActionReturnCode}
     */
    spawn(){
        if(this.size>=this.maxSize){
            errLog(new Error("excessive explorer"))
            return ERR_FULL;
        }
        // if(this.targetPos.roomName === 'W6N4'){
        //     console.log(Memory.rooms[this.targetPos.room].enemy, this.targetPos.roomName)
        // }
        if(Memory.rooms[this.targetPos.roomName].enemy){
            if(Game.time%1000!==888){
                return ERR_NOT_OWNER;
            }
        }
        let newCreep=spawnCreep(this.spawnRoom,{
            require:[MOVE]
        },{
            memory:{
                role:'explorer',
            }
        });
        if(!(newCreep instanceof Creep)){
            return newCreep;
        }
        this.addCreep(newCreep);
        return OK;
    }
    run(){
        let creep = Game.creeps[this.creepNames[0]];
        if(!creep || creep.spawning){
            return OK;
        }

        let controller=creep.room.controller;
        if((controller!=undefined && controller.owner!=undefined && 
                    controller.owner.username!==this.spawnRoom.controller.owner.username) ||
                (!controller && creep.room.find(FIND_HOSTILE_STRUCTURES,{filter:(structure)=>structure.structureType === STRUCTURE_TOWER && structure.owner !== undefined && structure.owner.username === "Invader"}).length>0)){
            // console.log(creep,creep.room);
            if(!creep.room.memory.enemy){
                let logString = "SCPS: enemy found in "+creep.room.name;
                console.log(logString);
                Game.notify(logString);
                creep.room.memory.enemy = true;
            }
        }else{
            creep.room.memory.enemy = undefined;
        }

        let ret;
        let pos=this.targetPos;
        pos=new RoomPosition(pos.x,pos.y,pos.roomName);
        ret=creep.moveTo(pos,{
            reusePath:50,
            visualizePathStyle:{stroke:'#00ffff'},
            plainCost:1,
            swampCost:1,
            ignoreRoads:true,
        });
        creep.say('expl:'+(ret===OK?this.targetPos.roomName:ret));
        return OK;
    }
}